﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmSTNApproval : Form
    {
        DataTable GvDatatable1 = new DataTable();
        DataTable GvDatatable = new DataTable();
        DataTable GvPLIDList = new DataTable();
        public frmSTNApproval()
        {
            InitializeComponent();
            //check box in dgvGRNApproval
            InitializeComponent();
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.HeaderText = "Check Item";
            check.FalseValue = "0";
            check.TrueValue = "1";
            check.Width = 20;
            dgvSTNMaster.Columns.Insert(0, check);

            //check box in dgvGRNdetailapproval
            DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
            check1.HeaderText = "Check Item";
            check1.FalseValue = "0";
            check1.TrueValue = "1";
            check1.Width = 20;
            dgvSTNDetails.Columns.Insert(0, check1);


            #region GvDatatable-Data Table

            GvDatatable.Columns.Add("STN_ID");
            GvDatatable.Columns.Add("PL_ID");
            GvDatatable.Columns.Add("Pro_ID");
            GvDatatable.Columns.Add("Pro_Name");
            GvDatatable.Columns.Add("Brand_Name");
            GvDatatable.Columns.Add("Ret_Price");
            GvDatatable.Columns.Add("Pur_Price");
            GvDatatable.Columns.Add("PV");
            GvDatatable.Columns.Add("BV");
            GvDatatable.Columns.Add("Status");
            GvDatatable.Columns.Add("Qty");
            GvDatatable.Columns.Add("Date_Create");
            GvDatatable.Columns.Add("User_Modify");
            GvDatatable.Columns.Add("Last_Modify");
               
            #endregion


            #region GvPLIDList-Data Table

            GvPLIDList.Columns.Add("STN_ID");
            GvPLIDList.Columns.Add("PL_ID");

            #endregion
        }
        private void frmSTNApproval_Load(object sender, EventArgs e)
        {
            PendingSTN();
        }


        //First Grid
        public void PendingSTN()
        {
            try
            {
                List<STN> LvList = new List<STN>();
                String LvPenList = "PENDING";
                LvList = STNDAO.GetAllSTNbyStatus(LvPenList);
                DataTable LvPRPending = new DataTable();
                dgvSTNMaster.DataSource = LvPRPending;

                #region LvPRPending--Datatable
                LvPRPending.Columns.Add("STN_ID");
                LvPRPending.Columns.Add("Branch_ID");
                LvPRPending.Columns.Add("Status");
                LvPRPending.Columns.Add("Issue_Date");
                LvPRPending.Columns.Add("Total");
                LvPRPending.Columns.Add("User_ID");
                LvPRPending.Columns.Add("Date_Create");
               
                #endregion


                foreach (STN Lvitem in LvList)
                {
                    LvPRPending = (DataTable)dgvSTNMaster.DataSource;
                    LvPRPending.Rows.Add(LvPRPending.NewRow());
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][0] = Lvitem.STN_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][1] = Lvitem.Branch_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][2] = Lvitem.status.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][3] = Lvitem.Issue_Date.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][4] = Lvitem.Total.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][5] = Lvitem.User_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][6] = Lvitem.Date_Create.ToString();
                  
                }


            }
            catch (Exception)
            {

            }
        }
        private void dgvSTNMaster_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvSTNMaster.Rows[dgvSTNMaster.CurrentRow.Index].Cells[0];

            string LvSTNid = dgvSTNMaster.Rows[e.RowIndex].Cells[1].Value.ToString();


            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    STNDetailFillGrid(LvSTNid);//Fill Second Grid


                    break;
            }
        }
       
            

        //Second Grid
        private void STNDetailFillGrid(string PSTNid)
        {
            String LvStatus = "PENDING";
            GvDatatable1 = STNDAO.GetAllSTNDetailsbySTNid(PSTNid, LvStatus);


            for (int i = 0; i <= GvDatatable1.Rows.Count - 1; i++)
            {
                GvDatatable.Rows.Add(GvDatatable.NewRow());
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][0] = GvDatatable1.Rows[i][0].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][1] = GvDatatable1.Rows[i][1].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][2] = GvDatatable1.Rows[i][2].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][3] = GvDatatable1.Rows[i][3].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][4] = GvDatatable1.Rows[i][4].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][5] = GvDatatable1.Rows[i][5].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][6] = GvDatatable1.Rows[i][6].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][7] = GvDatatable1.Rows[i][7].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][8] = GvDatatable1.Rows[i][8].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][9] = GvDatatable1.Rows[i][9].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][10] = GvDatatable1.Rows[i][10].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][11] = GvDatatable1.Rows[i][11].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][12] = GvDatatable1.Rows[i][12].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][13] = GvDatatable1.Rows[i][13].ToString();
            }

            dgvSTNDetails.DataSource = GvDatatable;

        }

        private void dgvSTNDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvSTNDetails.Rows[dgvSTNDetails.CurrentRow.Index].Cells[0];

            string LvPLid = dgvSTNDetails.Rows[e.RowIndex].Cells[2].Value.ToString();
            //string LvStatus = dgvPOdetailsapproval.Rows[e.RowIndex].Cells[4].Value.ToString();
            string LvSTNid = dgvSTNDetails.Rows[e.RowIndex].Cells[1].Value.ToString();
            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    PLListToApprove(LvSTNid, LvPLid);//Add to List


                    break;
            }
        }
       
        #region Save
        private void PLListToApprove(string PSTNID, string PPLid)
        {

            GvPLIDList.Rows.Add(GvPLIDList.NewRow());
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][0] = PSTNID;
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][1] = PPLid;

        }

      
        private void btnSTNSubmit_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("podetails");

            string detailsXML = "";

            ds.Tables.Add(GvPLIDList.Copy());
            detailsXML = ds.GetXml();

            STNApprovalList(detailsXML);
            MessageBox.Show("GRN Was Succesfuly Saved");
        }

        private void STNApprovalList(string xmlstring)
        {
            string Lvxml = xmlstring;
            string Lvstatus = null;
            if (rbSTNCancel.Checked)
            {
                Lvstatus = "CANCELLED";
            }
            else
                if (rbSTNApprove.Checked)
                {
                    Lvstatus = "APPROVED";
                }

            int Lvresult = STNDAO.UpdateSTN(Lvxml, Lvstatus);



            if (Lvresult != 0 && Lvstatus == "APPROVED")
            {
                MessageBox.Show("GRN Approved Sucessfully");
            }
            else
                if (Lvresult != 0 && Lvstatus == "CANCELLED")
                {
                    MessageBox.Show("GRN Cancelled Sucessfully");
                }
        }

        #endregion


      

       

       

       

    }
}
